# -*- coding: utf-8 -*-

"""
scrapy 配置
"""

# Scrapy settings for spiders project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
#     https://doc.scrapy.org/en/latest/topics/settings.html
#     https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
#     https://doc.scrapy.org/en/latest/topics/spider-middleware.html

BOT_NAME = 'spiders'

SPIDER_MODULES = ['spiders.spiders']
NEWSPIDER_MODULE = 'spiders.spiders'

# Crawl responsibly by identifying yourself (and your website) on the user-agent
# USER_AGENT = 'spiders (+http://www.yourdomain.com)'

# Obey robots.txt rules
ROBOTSTXT_OBEY = False

# Configure maximum concurrent requests performed by Scrapy (default: 16)
# CONCURRENT_REQUESTS = 32

# Configure a delay for requests for the same website (default: 0)
# See https://doc.scrapy.org/en/latest/topics/settings.html#download-delay
# See also autothrottle settings and docs
# DOWNLOAD_DELAY = 3
# The download delay setting will honor only one of:
# CONCURRENT_REQUESTS_PER_DOMAIN = 16
# CONCURRENT_REQUESTS_PER_IP = 16

# Disable cookies (enabled by default)
# COOKIES_ENABLED = False

# Disable Telnet Console (enabled by default)
# TELNETCONSOLE_ENABLED = False

# Override the default request headers:
# DEFAULT_REQUEST_HEADERS = {
#   'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
#   'Accept-Language': 'en',
# }

# Enable or disable spider middlewares
# See https://doc.scrapy.org/en/latest/topics/spider-middleware.html
# SPIDER_MIDDLEWARES = {
#    'spiders.middlewares.SpidersSpiderMiddleware': 543,
# }

# Enable or disable downloader middlewares
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
# DOWNLOADER_MIDDLEWARES = {
#    'spiders.middlewares.SpidersDownloaderMiddleware': 543,
# }

# Enable or disable extensions
# See https://doc.scrapy.org/en/latest/topics/extensions.html
# EXTENSIONS = {
#    'scrapy.extensions.telnet.TelnetConsole': None,
# }

# Configure item pipelines
# See https://doc.scrapy.org/en/latest/topics/item-pipeline.html
# ITEM_PIPELINES = {
#    'spiders.pipelines.SpidersPipeline': 300,
# }
ITEM_PIPELINES = {
    'spiders.pipelines.HuoBiPipeline.HuoBiPipeline': 300,
    'spiders.pipelines.GatePipeline.GatePipeline': 300,
    'spiders.pipelines.OkexPipeline.OkexPipeline': 200,
    'spiders.pipelines.JinSePipeline.JinSePipeline': 300,
    'spiders.pipelines.JsonWithEncodingPipeline.JsonWithEncodingPipeline': 300,  # 保存到JSON文件中
    'scrapy_redis.pipelines.RedisPipeline': 300,
}

# Enable and configure the AutoThrottle extension (disabled by default)
# See https://doc.scrapy.org/en/latest/topics/autothrottle.html
# AUTOTHROTTLE_ENABLED = True
# The initial download delay
# AUTOTHROTTLE_START_DELAY = 5
# The maximum download delay to be set in case of high latencies
# AUTOTHROTTLE_MAX_DELAY = 60
# The average number of requests Scrapy should be sending in parallel to
# each remote server
# AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
# Enable showing throttling stats for every response received:
# AUTOTHROTTLE_DEBUG = False

# Enable and configure HTTP caching (disabled by default)
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
# HTTPCACHE_ENABLED = True
# HTTPCACHE_EXPIRATION_SECS = 0
# HTTPCACHE_DIR = 'httpcache'
# HTTPCACHE_IGNORE_HTTP_CODES = []
# HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'

"""
mysql 配置
"""
MYSQL_HOST = '127.0.0.1'
MYSQL_DBNAME = 'spiders'
MYSQL_USER = 'root'
MYSQL_PASSWD = 'root'
MYSQL_PORT = 3306

"""
scrapy_redis 配置
"""
# 主机名
REDIS_HOST = '127.0.0.1'
# 端口
REDIS_PORT = 6379
# 连接URL（优先于以上配置）
# REDIS_URL = 'redis://user:pass@hostname:9001'

# Redis连接参数 默认(如下)：
# REDIS_PARAMS = {
#   'socket_timeout': 30,
#   'socket_connect_timeout': 30,
#   'retry_on_timeout': True,
#   'encoding': REDIS_ENCODING,
# }

# 这个设置了不管用，我也不知道为什么，爬虫单独设置即可
REDIS_PARAMS = {
   'password': 'Aa123456',
   'db': 0,
},

# 指定连接Redis的Python模块 默认：redis.StrictRedis
# REDIS_PARAMS['redis_cls'] = 'redis.StrictRedis'
# REDIS_CLS = 'redis.StrictRedis'
# redis编码类型 默认：'utf-8'
REDIS_ENCODING = "utf-8"

# 默认使用优先级队列（默认），其他：PriorityQueue（有序集合），FifoQueue（列表）、LifoQueue（列表）
SCHEDULER_QUEUE_CLASS = 'scrapy_redis.queue.PriorityQueue'
# 调度器中请求存放在redis中的key
SCHEDULER_QUEUE_KEY = '%(spider)s:requests'
# 对保存到redis中的数据进行序列化，默认使用pickle
SCHEDULER_SERIALIZER = "scrapy_redis.picklecompat"
# 是否在关闭时候保留原来的调度器和去重记录，True=保留，False=清空
SCHEDULER_PERSIST = True
# 是否在开始之前清空 调度器和去重记录，True=清空，False=不清空
SCHEDULER_FLUSH_ON_START = False
# 去调度器中获取数据时，如果为空，最多等待时间（最后没数据，未获取到）
SCHEDULER_IDLE_BEFORE_CLOSE = 10

# 去重规则，在redis中保存时对应的key 例："命名空间：key"
SCHEDULER_DUPEFILTER_KEY = '%(spider)s:dupefilter'
"""
这是示例
gate:dupefilter:{}
okex:dupefilter:{}
"""

# 去重规则对应处理的类
SCHEDULER_DUPEFILTER_CLASS = 'scrapy_redis.dupefilter.RFPDupeFilter'
# 调度器使用scrapy_redis
SCHEDULER = "scrapy_redis.scheduler.Scheduler"
# 去重使用 scrapy_redis
DUPEFILTER_CLASS = "scrapy_redis.dupefilter.RFPDupeFilter"

# 起始URL
# 获取起始URL时，去集合中获取还是去列表中获取？True从集合获取，False从列表获取
# 编写爬虫时，起始URL从redis的Key中获取
REDIS_START_URLS_AS_SET = False
REDIS_START_URLS_KEY = '%(name)s:start_urls'

"""
批量启动配置
"""
COMMANDS_MODULE = 'commands'
